package com.xinyun.client;

import com.xinyun.server.protocol.ProtocolData;
import com.xinyun.server.protocol.ProtocolDataHeader;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created by bubusy on 16/5/21.
 */
public class ProtocolEncoder extends MessageToByteEncoder<ProtocolData> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ProtocolData protocolData, ByteBuf byteBuf) throws Exception {
        ProtocolDataHeader header = protocolData.getHeader();
        byteBuf.writeShort(header.getLength());
        byteBuf.writeByte(header.getMsgType());
        byteBuf.writeByte(header.getB4());
        byteBuf.writeByte(header.getB5());
        byteBuf.writeByte(header.getB6());
        byteBuf.writeBytes(protocolData.getBodyBytes());
    }
}
